package com.gree.scmservice.controller;

import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.SysDictType;
import com.gree.common.entity.TablePageData;
import com.gree.common.enums.BusinessType;
import com.gree.common.utils.ExcelUtil;
import com.gree.scmservice.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by 180686 on 2021/3/24 8:46
 */

@RestController
@RequestMapping("/dict/type")
@Api(tags = "系统信息:字典类型")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo list(SysDictType dictType)
    {
        TablePageData<SysDictType> sysDictTypeTablePageData = sysDictTypeService.selectDictTypeList(dictType);
        return ReturnVo.success(sysDictTypeTablePageData);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出字典类型")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "SysDictType")
    public void export(SysDictType dictType, HttpServletResponse response)
    {
        TablePageData<SysDictType> sysDictTypeTablePageData = sysDictTypeService.selectDictTypeList(dictType);
        List<SysDictType> list = sysDictTypeTablePageData.getDataList();
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(list, "字典类型", response);
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictId}")
    @ApiOperation(value = "查询字典类型详细")
    @ApiImplicitParam(name = "dictId", value = "字典类型ID", dataType = "Long")
    public ReturnVo getInfo(@PathVariable Long dictId)
    {
        return ReturnVo.success(sysDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增字典类型")
    @ApiImplicitParam(name = "dict", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo add(@Validated @RequestBody SysDictType dict)
    {
        if (sysDictTypeService.checkDictTypeUnique(dict))
        {
            return ReturnVo.error("新增字典'" + dict.getDict_name() + "'失败，字典类型已存在");
        }
        return ReturnVo.toAjax(sysDictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改字典类型")
    @ApiImplicitParam(name = "dict", value = "数据字典类型", dataType = "SysDictType")
    public ReturnVo edit(@Validated @RequestBody SysDictType dict)
    {
        if (sysDictTypeService.checkDictTypeUnique(dict))
        {
            return ReturnVo.error("修改字典'" + dict.getDict_name() + "'失败，字典类型已存在");
        }
        return ReturnVo.toAjax(sysDictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "dictIds", value = "字典类型ID", dataType = "Long[]")
    public ReturnVo remove(@PathVariable Long[] dictIds)
    {
        int resultNum = sysDictTypeService.deleteDictTypeByIds(dictIds);
        return ReturnVo.toAjax(resultNum);
    }

    /**
     * 清空缓存
     */
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    @ApiOperation(value = "清空缓存")
    public ReturnVo clearCache()
    {
        sysDictTypeService.clearCache();
        return ReturnVo.success();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    @ApiOperation(value = "获取字典选择框列表")
    public ReturnVo optionselect()
    {
        List<SysDictType> dictTypes = sysDictTypeService.selectDictTypeAll();
        return ReturnVo.success(dictTypes);
    }
}
